package com.css.apps.base.org.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.common.provider.json.OrgJsonProvider;
import com.css.apps.base.org.model.SOrg;
import com.css.core.configuration.Environment;
import com.css.core.model.tree.Tree;
import com.css.core.model.tree.TreeCache;
import com.css.core.model.tree.TreeNode;
import com.css.db.query.QueryCache;
import com.css.util.Ajax;
import com.css.util.Messages;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 获取自定义组织机构树
 */
@SuppressWarnings("serial")
public class GetPreviewTree extends AdminAction {

	private static Log log = LogFactory.getLog(GetOrgTree.class);
	private String parentId;
	private Integer series;
	private List<String> sysList;

	@Override
	protected String adminGo() {
		try {
			JSONArray orgTree = getOrgTree(series, parentId);
			result = orgTree.toString();
			return Action.SUCCESS;
		} catch (Exception ex) {
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.exception"));
			log.error(ex.getMessage(), ex);
			return Action.ERROR;
		}
	}

	public static JSONArray getOrgTree(Integer series, String parentId) {
		JSONArray orgTree = new JSONArray();
		JSONObject one = new JSONObject();
		List<TreeNode> lst = new ArrayList<TreeNode>();
		Tree tree = new TreeCache(SOrg.class);
		TreeNode tn = tree.getTreeNode(parentId);// 当前节点
		if (series == null) {
			OrgJsonProvider json = new OrgJsonProvider();
			orgTree = json.getSubOrgan(parentId, null);
			if (tn != null) {
				SOrg item = QueryCache.get(SOrg.class, tn.getNodeId());
				if (item != null) {
					one = new JSONObject();
					one.put("id", tn.getNodeId());
					one.put("name", item.getName());
					one.put("pId", item.getParentId());
					one.put("isParent", false);
					one.put("open", true);
					orgTree.add(one);
				}
			}
		} else {
			List<TreeNode> nds = tn.getChildren();// 当前节点的孩子节点
			lst.addAll(nds);
			for (int i = 1; i < series; i++) {
				lst.addAll(getChilds(nds));
				nds = getChilds(nds);
			}
			lst.add(tn);
			for (TreeNode node : lst) {
				SOrg item = QueryCache.get(SOrg.class, node.getNodeId());
				if (item != null) {
					one = new JSONObject();
					one.put("id", node.getNodeId());
					one.put("name", item.getName());
					one.put("pId", item.getParentId());
					one.put("isParent", false);
					one.put("open", true);
					orgTree.add(one);
				}
			}
		}
		return orgTree;
	}

	public static List<TreeNode> getChilds(List<TreeNode> nds) {
		List<TreeNode> list = null;
		List<TreeNode> listAll = new ArrayList<TreeNode>();
		for (TreeNode node : nds) {
			if (!node.getChildren().isEmpty()) {
				list = node.getChildren();
				listAll.addAll(list);
			}
		}
		return listAll;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public String getParentId() {
		return parentId;
	}

	public Integer getSeries() {
		return series;
	}

	public void setSeries(Integer series) {
		this.series = series;
	}

	public List<String> getSysList() {
		return sysList;
	}

	public void setSysList(List<String> sysList) {
		this.sysList = sysList;
	}

}
